package com.lbj.googleplay.googleplay100.ui.holder;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lbj.googleplay.googleplay100.R;
import com.lbj.googleplay.googleplay100.domain.AppInfo;
import com.lbj.googleplay.googleplay100.utils.UIUtils;

public class DetailDesHolder extends BaseHolder<AppInfo> {

    private ImageView ivArrow;
    private TextView tvDes;
    private TextView tvAuthor;
    private RelativeLayout rlDetailToggle;
    private LinearLayout.LayoutParams params;

    @Override
    public View initView() {
        View view = UIUtils.inflate(R.layout.layout_detail_desinfo);
        ivArrow = view.findViewById(R.id.iv_arrow);
        tvDes = view.findViewById(R.id.tv_detail_des);
        tvAuthor = view.findViewById(R.id.tv_detail_author);
        rlDetailToggle = view.findViewById(R.id.rl_detail_toggle);
        rlDetailToggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggle(isOpen);
            }
        });
        return view;
    }

    private  boolean isOpen = false;

    private void toggle(boolean isOpen) {
        int shortHeight = getShortHeight();
        int longHeight = getLongHeight();
        ValueAnimator animator = null;
        if (isOpen){
            isOpen = false;
            animator = ValueAnimator.ofInt(longHeight,shortHeight);
        }else {
            isOpen = true;
            animator = ValueAnimator.ofInt(shortHeight,longHeight);
        }
        if (animator!=null) {
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int currentHeight = (int) animation.getAnimatedValue();
                    params.height = currentHeight;
                    tvDes.setLayoutParams(params);
                }
            });
            final boolean finalIsOpen = isOpen;
            animator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    //动画结束,让scrollView滑动到最底部
                    final ScrollView view = getScrollView();
                    view.post(new Runnable() {
                        @Override
                        public void run() {
                            view.fullScroll(ScrollView.FOCUS_DOWN);
                        }
                    });
                    //动画结束后,改变箭头方向
                    if (finalIsOpen){
                        ivArrow.setImageResource(R.drawable.arrow_up);
                    }else {
                        ivArrow.setImageResource(R.drawable.arrow_down);
                    }
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
        animator.setDuration(200);
        animator.start();

        }
    }

    @Override
    public void refreshView(AppInfo data) {
        tvDes.setText(data.des);
        tvAuthor.setText(data.author);
        tvDes.post(new Runnable() {
            @Override
            public void run() {
                //默认展示7行高度
                int shortHeight = getShortHeight();
                params = (LinearLayout.LayoutParams) tvDes.getLayoutParams();
                params.height = shortHeight;
                tvDes.setLayoutParams(params);
            }
        });



    }

    /*
    * 获取7行textView文字的高度
    * */
    public int getShortHeight(){
        //模拟一个textView,设置最大行数为7行,计算该虚拟textView的高度,从而未知道tvDes展示7行的高度
        int width = tvDes.getMeasuredWidth();

        TextView view = new TextView(UIUtils.getContext());
        view.setText(getData().des);
        view.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        view.setMaxLines(7);
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(2000, View.MeasureSpec.AT_MOST);
        //开始测量
        view.measure(widthMeasureSpec,heightMeasureSpec);
        int height = view.getMeasuredHeight();

        return height;//返回测量后的高度
    }

    /*
     * 获取textView文字的完整高度
     * */
    public int getLongHeight(){
        //模拟一个textView,设置最大行数为7行,计算该虚拟textView的高度,从而未知道tvDes展示7行的高度
        int width = tvDes.getMeasuredWidth();

        TextView view = new TextView(UIUtils.getContext());
        view.setText(getData().des);
        view.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
//        view.setMaxLines(7);
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(2000, View.MeasureSpec.AT_MOST);
        //开始测量
        view.measure(widthMeasureSpec,heightMeasureSpec);
        int height = view.getMeasuredHeight();

        return height;//返回测量后的高度
    }

    private ScrollView getScrollView(){

        ViewParent parent = tvDes.getParent();

        while (!(parent instanceof ScrollView)){
                parent = tvDes.getParent();
        }

        return (ScrollView) parent;
    }
}
